home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / game.c < prev    next >
C/C++ Source or Header  |  1993-06-22  |  10KB  |  423 lines

  1. /*
  2.  * postprint.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23. #include <stdio.h>
  24. #include "gnuchess.h"
  25. #ifdef MSDOS
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #define RWA_ACC "r+b"
  30. #define WA_ACC "w+b"
  31. #else
  32. #define RWA_ACC "r+"
  33. #define WA_ACC "w+"
  34. #include <sys/param.h>
  35. #include <sys/types.h>
  36. #endif /* MSDOS */
  37. FILE *fd;
  38.  
  39. #define truescore 0x0001
  40. #define lowerbound 0x0002
  41. #define upperbound 0x0004
  42. #define kingcastle 0x0008
  43. #define queencastle 0x0010
  44. const short otherside[3] =
  45. {black, white, neutral};
  46. const short Stboard[64] =
  47. {rook, knight, bishop, queen, king, bishop, knight, rook,
  48.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  49.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  52.  rook, knight, bishop, queen, king, bishop, knight, rook};
  53. const short Stcolor[64] =
  54. {white, white, white, white, white, white, white, white,
  55.  white, white, white, white, white, white, white, white,
  56.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  57.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  58.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  59.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  60.  black, black, black, black, black, black, black, black,
  61.  black, black, black, black, black, black, black, black};
  62.  
  63. struct GameRec GameList[512];
  64. char mvstr[4][6];
  65. long i, j;
  66. int nr;
  67. short int ep;
  68. int r, c;
  69. char line[128];
  70. char *l;
  71. short int board[64];
  72. short int color[64];
  73. short int GameCnt;
  74. int from, to;
  75. char *InPtr;
  76.  
  77. void
  78. skip ()
  79. {
  80.   while (*InPtr != ' ')
  81.     InPtr++;
  82.   while (*InPtr == ' ')
  83.     InPtr++;
  84. }
  85. void
  86. skipb ()
  87. {
  88.   while (*InPtr == ' ')
  89.     InPtr++;
  90. }
  91. int
  92. parser (char *f, int side, unsigned short *flags)
  93. {
  94.   int c1, r1, c2, r2;
  95.  
  96.   *flags = 0;
  97.  
  98.   if (f[4] == 'o')
  99.     if (side == black)
  100.       return 0x3C3A;
  101.     else
  102.       return 0x0402;
  103.   else if (f[0] == 'o')
  104.     if (side == black)
  105.       return 0x3C3E;
  106.     else
  107.       return 0x0406;
  108.   else
  109.     {
  110.       c1 = f[0] - 'a';
  111.       r1 = f[1] - '1';
  112.       c2 = f[2] - 'a';
  113.       r2 = f[3] - '1';
  114.       if (f[4] != ' ')
  115.     {
  116.       /* promotion */
  117.       for (i = 0; i < 7; i++)
  118.         if (f[4] == Qxx[i])
  119.           {
  120.         *flags = (unsigned short)i | promote;
  121.         break;
  122.           }
  123.     }
  124.       return (locn (r1, c1) << 8) | locn (r2, c2);
  125.     }
  126.   return (0);
  127. }
  128.  
  129. void
  130. algbr (short int f, short int t, short int flag)
  131.  
  132.  
  133. /*
  134.  * Generate move strings in different formats.
  135.  */
  136.  
  137. {
  138.   int m3p;
  139.  
  140.   if (f != t)
  141.     {
  142.       /* algebraic notation */
  143.       mvstr[0][0] = Cxx[column (f)];
  144.       mvstr[0][1] = Rxx[row (f)];
  145.       mvstr[0][2] = Cxx[column (t)];
  146.       mvstr[0][3] = Rxx[row (t)];
  147.       mvstr[0][4] = mvstr[3][0] = '\0';
  148.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  149.     {
  150.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  151.         {
  152.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  153.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  154.           m3p = 2;
  155.         }
  156.       else
  157.         /* pawn ate */
  158.         {
  159.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  160.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  161.           mvstr[2][2] = mvstr[0][3];
  162.           m3p = 3;        /* to row */
  163.         }
  164.       if (flag & promote)
  165.         {
  166.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  167.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  168. #ifdef CHESSTOOL
  169.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  170.           mvstr[3][1] = mvstr[0][1];
  171.           mvstr[3][2] = mvstr[0][2];
  172.           mvstr[3][3] = mvstr[0][3];
  173.           mvstr[3][4] = '\0';
  174. #endif
  175.         }
  176.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  177.     }
  178.       else
  179.     /* not a pawn */
  180.     {
  181.       mvstr[2][0] = mvstr[1][0];
  182.       mvstr[2][1] = mvstr[0][1];
  183.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  184.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  185.       mvstr[2][4] = mvstr[1][3] = '\0';
  186.       strcpy (mvstr[3], mvstr[2]);
  187.       mvstr[3][1] = mvstr[0][0];
  188.       if (flag & cstlmask)
  189.         {
  190.           if (t > f)
  191.         {
  192.           strcpy (mvstr[1], "o-o");
  193.           strcpy (mvstr[2], "O-O");
  194.         }
  195.           else
  196.         {
  197.           strcpy (mvstr[1], "o-o-o");
  198.           strcpy (mvstr[2], "O-O-O");
  199.         }
  200.         }
  201.     }
  202.     }
  203.   else
  204.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  205. }
  206.  
  207. void
  208. GetGame ()
  209. {
  210.   char fb[256];
  211.   unsigned short flags;
  212.  
  213.   fgets (fb, 256, fd);
  214.   fgets (fb, 256, fd);
  215.   while (fgets (fb, 256, fd))
  216.     {
  217.       struct GameRec *g;
  218.       int side = white;
  219.  
  220.       side = otherside[side];
  221.       if (fb[0] == '\n')
  222.     return;
  223.       ++GameCnt;
  224.       InPtr = fb;
  225.       skipb ();
  226.       g = &GameList[GameCnt];
  227.       g->gmove = parser (InPtr, side, &flags);
  228.       skip ();
  229.       g->score = atoi (InPtr);
  230.       skip ();
  231.       g->depth = atoi (InPtr);
  232.       skip ();
  233.       g->nodes = atol (InPtr);
  234.       skip ();
  235.       g->time = atol (InPtr);
  236.       g->flags = flags;
  237.       skip ();
  238.       ++GameCnt;
  239.       g = &GameList[GameCnt];
  240.       g->gmove = parser (InPtr, side, &flags);
  241.       skip ();
  242.       g->score = atoi (InPtr);
  243.       skip ();
  244.       g->depth = atoi (InPtr);
  245.       skip ();
  246.       g->nodes = atol (InPtr);
  247.       skip ();
  248.       g->time = atol (InPtr);
  249.       g->flags = flags;
  250.  
  251.     }
  252. }
  253. short int xside, side;
  254. void
  255. getboard (int mvno)
  256.  
  257. {
  258.   register short int f, t;
  259.   short int rf, rt;
  260.   unsigned short mv;
  261.  
  262.   /* now update the board and hash values */
  263.  
  264.   /*
  265.    * should really check the moves as we do this, but???
  266.    */
  267.   mv = GameList[mvno].gmove;
  268.   f = mv >> 8 & 0x7F;
  269.   t = mv & 0xFF;
  270.   /* can only capture other side */
  271.   if (board[t] != no_piece)
  272.     {
  273.       if (color[t] != xside)
  274.     {
  275.       algbr (f, t, 0);
  276.       printf ("Illegal move - %d %s \n", mvno, mvstr[0]);
  277.     }
  278.     }
  279.   /* there must be a piece to move */
  280.   if (board[f] == no_piece || color[f] != side)
  281.     {
  282.       algbr (f, t, 0);
  283.       printf ("Illegal move + %d %s \n", mvno, mvstr[0]);
  284.     }
  285.   /* is it EnPassant */
  286.   if (board[f] == pawn && board[t] == no_piece)
  287.     {
  288.       if ((row (f) == 4 && row (t) == 3) || (row (f) == 5 && row (t) == 6))
  289.     {
  290.       if ((column (t) == column (f) + 1)
  291.           || (column (t) == column (f) - 1))
  292.         {
  293.           ep = t + ((t > f) ? -8 : 8);
  294.           if (board[ep] == pawn && color[ep] == xside)
  295.         {
  296.           board[ep] = no_piece;
  297.           color[ep] = neutral;
  298.         }
  299.         }
  300.     }
  301.     }
  302.   board[t] = board[f];
  303.   color[t] = color[f];
  304.   color[f] = neutral;
  305.   board[f] = no_piece;
  306.   /* castle moves */
  307.   if ((board[t] == king) & ((mv == BLACKCASTLE) || (mv == WHITECASTLE) || (mv == LONGBLACKCASTLE) || (mv == LONGWHITECASTLE)))
  308.     {
  309.  
  310.       if (t > f)
  311.     {
  312.       rf = f + 3;
  313.       rt = t - 1;
  314.     }
  315.       else
  316.     {
  317.       rf = f - 4;
  318.       rt = t + 1;
  319.     }
  320.       board[rt] = rook;
  321.       color[rt] = side;
  322.       board[rf] = no_piece;
  323.       color[rf] = neutral;
  324.     }
  325.   else if (GameList[i].flags & promote)
  326.  
  327.     board[t] = GameList[i].flags & pmask;
  328.   xside = side;
  329.   side = otherside[side];
  330. }
  331.  
  332. void
  333. main (int argc, char **argv)
  334. {
  335.   int from, to;
  336.   int f = 0;
  337.   unsigned short int mv;
  338.   int start, end;
  339.  
  340.   if (argc > 4 || argc < 2)
  341.     {
  342.       printf ("Usage: game file [start [end] ] \n");
  343.       exit (1);
  344.     }
  345.   start = end = 0;
  346.   if (argc > 2)
  347.     start = (atoi (argv[2]) * 2) - 1;
  348.   if (argc == 4)
  349.     end = (atoi (argv[3]) * 2) - 1;
  350.   side = white;
  351.   xside = black;
  352.   for (i = 0; i < 64; i++)
  353.     {
  354.       board[i] = Stboard[i];
  355.       color[i] = Stcolor[i];
  356.     }
  357.   i = 1;
  358.   if ((fd = fopen (argv[1], RWA_ACC)) == NULL)
  359.     exit (1);
  360.   printf ("/V 11 72 mul def /L 60 def\n");
  361.   GetGame ();
  362.   if (!start || start < 1 || start > GameCnt)
  363.     start = 1;
  364.   if (!end || end > GameCnt || end < 1)
  365.     end = GameCnt;
  366.   for (i = 1; i < end; i++)
  367.     {
  368.       getboard ((int)i);
  369.       if (i < start)
  370.     continue;
  371.       nr++;
  372.       if (nr == 19)
  373.     {
  374.       nr = 1;
  375.       printf ("showpage\n/V 11 72 mul def\n");
  376.       printf ("/L 60 def\n");
  377.       f = 0;
  378.     }
  379.       /* now process this entry */
  380.       strcpy (line, "C ('#[");
  381.       for (r = 0; r < 8; r++)
  382.     {
  383.       l = line + 6 + (7 - r) * 9;
  384.       for (c = 0; c < 8; c++)
  385.         {
  386.           if (color[r * 8 + c] == black)
  387.         *l++ = Qxx[board[r * 8 + c]];
  388.           else
  389.         *l++ = Pxx[board[r * 8 + c]];
  390.         }
  391.       *l++ = ';';
  392.     }
  393.       l--;
  394.       line[79] = '\0';
  395.       strcat (line, "]') show");
  396.       /* decode flags */
  397.       printf ("L V moveto\n");
  398.       mv = GameList[i].gmove;
  399.       from = mv >> 8 & 0x7F;
  400.       to = mv & 0x7F;
  401.       algbr (from, to, 0);
  402.       if (i % 2)
  403.     printf ("R (%d %s score %d time %d", (i + 1) / 2, mvstr[0], GameList[i].score, GameList[i].time);
  404.       else
  405.     printf ("R (%d  ... %s score %d time %d", (i + 1) / 2, mvstr[0], GameList[i].score, GameList[i].time);
  406.       printf (") show\n");
  407.       printf ("L  V 100 sub moveto\n");
  408.       printf ("%s\n", line);
  409.       f++;
  410.       if (f == 3)
  411.     {
  412.       printf ("/V V 120 sub def /L 60 def\n");
  413.       f = 0;
  414.     }
  415.       else
  416.     printf ("/L 160 L add def\n");
  417.     }
  418.  
  419.   if (nr)
  420.     printf ("showpage\n");
  421.   exit (0);
  422. }
  423.